﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Markup;
// ColorPicker, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace ColorPicker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private string getColorCS(List<ColorsCooked> lck)
        {
            string s = "";
            foreach (ColorsCooked c in lck)
            {
                s += getColorCS(c);
            }
            return s;
        }
        private string getColorCS(ColorsCooked c)
        {
            ColorNameWithSource v = c.GetWinColor();
            if ("A" == v.Source)
            {
                return string.Format("Color Color{0} = Color.FromRgb({1},{2},{3});//{4}\n", CleanString.CleanName(v.Name), c.ColorR, c.ColorG, c.ColorB, v.Name);
            }
            else
            {
                return string.Format("Color Color{0} = Colors.{1};//{2},{3},{4}\n", CleanString.CleanName(v.Name), v.Name, c.ColorR, c.ColorG, c.ColorB);
            }
        }

        private string getColorHTML(List<ColorsCooked> lck)
        {
            string s = "";
            foreach (ColorsCooked c in lck)
            {
                s += getColorHTML(c);
            }
            return s;
        }
        private string getColorHTML(ColorsCooked c)
        {
            return string.Format("rgb({0},{1},{2}) <!--{3}-->\n", c.ColorR, c.ColorG, c.ColorB, c.GetWinColor().Name);
        }

        private string getColorXAML(List<ColorsCooked> lck)
        {
            string s = "";
            foreach (ColorsCooked c in lck)
            {
                s += getColorXAML(c);
            }
            return s;
        }
        private string getColorXAML(ColorsCooked c)
        {
            ColorNameWithSource v = c.GetWinColor();
            if ("A" == v.Source)
            {
                return string.Format("<Color x:Key=\"Color{0}\" R=\"{1}\" G=\"{2}\" B=\"{3}\"/><!--{4}-->\n", CleanString.CleanName(v.Name), c.ColorR, c.ColorG, c.ColorB, v.Name);
            }
            else
            {
                return string.Format("<Color x:Key=\"Color{0}\">{0}</Color><!--{1},{2},{3}-->\n", v.Name, c.ColorR, c.ColorG, c.ColorB);
            }
        }

        private string getBrushXAML(List<ColorsCooked> lck)
        {
            string s = "";
            foreach (ColorsCooked c in lck)
            {
                s += getBrushXAML(c);
            }
            return s;
        }
        private string getBrushXAML(ColorsCooked c)
        {
            ColorNameWithSource v = c.GetWinColor();
            if ("A" == v.Source)
            {
                return string.Format("<SolidColorBrush x:Key=\"Brush{0}\" Color=\"#{1:X2}{2:X2}{3:X2}\"/><!--{4}-->\n", CleanString.CleanName(v.Name), c.ColorR, c.ColorG, c.ColorB, v.Name);
            }
            else
            {
                return string.Format("<SolidColorBrush x:Key=\"Brush{0}\" Color=\"{0}\"/><!--{1},{2},{3}-->\n", v.Name, c.ColorR, c.ColorG, c.ColorB);
            }
        }

        private string getGradientXXAML(ColorsCooked c, string offset)
        {
            ColorNameWithSource v = c.GetWinColor();
            if ("A" == v.Source)
            {
                //return string.Format("<GradientStop Color=\"{{StaticResource Color{0}}}\" Offset=\"{1}\"/>", c.CodeName, offset);
                return string.Format("<GradientStop Color=\"#{0:X2}{1:X2}{2:X2}\" Offset=\"{3}\"/><!--{4}-->", c.ColorR, c.ColorG, c.ColorB, offset, v.Name);
            }
            else
            {
                return string.Format("<GradientStop Color=\"{0}\" Offset=\"{1}\"/>", v.Name, offset);
            }
        }

        private string gradientBrush(List<ColorsCooked> cc)
        {
            string[] offsets = new string[cc.Count];
            double step = 1.0 / (cc.Count - 1);
            string points = "";
            //(0,0) - (1,0)
            //(0,1) - (1,1)
            switch (gradientKind.SelectedIndex)
            {
                case 0://Horizontal
                    points = "StartPoint=\"0,0\" EndPoint=\"1,0\"";
                    break;
                case 1://Vertical
                    points = "StartPoint=\"0,0\" EndPoint=\"0,1\"";
                    break;
                case 2://Diagonal Up-Down
                    points = "StartPoint=\"0,0\" EndPoint=\"1,1\"";
                    break;
                case 3:
                    points = "StartPoint=\"0,1\" EndPoint=\"1,0\"";
                    break;
            }

            offsets[0] = "0.0";
            offsets[cc.Count - 1] = "1.0";
            for (int i = 1; i < cc.Count - 1; i++)
            {
                offsets[i] = Math.Round(i * step, 2).ToString(System.Globalization.CultureInfo.InvariantCulture);
            }
            //
            //
            string s = "";
            //
            s += Environment.NewLine;
            //
            if (4 != gradientKind.SelectedIndex) s += string.Format("<LinearGradientBrush x:Key=\"_GradientBrush\" {0}>\n", points);
            else s += string.Format("<RadialGradientBrush x:Key=\"_RadialGradientBrush\" GradientOrigin=\"0.5,0.5\" Center=\"0.5,0.5\" RadiusX=\"0.5\" RadiusY=\"0.5\">\n");
            //
            for (int i = 0; i < cc.Count; i++)
            {
                s += "\t" + getGradientXXAML(cc[i], offsets[i]) + "\n";
            }
            //
            if (4 != gradientKind.SelectedIndex) s += "</LinearGradientBrush>";
            else s += "</RadialGradientBrush>";
            //
            s += "\n";
            //
            return s;
        }

    }//Class
}//namespace
